package com.dream.wuliu.view;

import org.androidannotations.annotations.App;
import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.UiThread;
import org.androidannotations.annotations.sharedpreferences.Pref;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Parcelable;
import android.widget.Toast;

import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.dream.vo.api.ApiUser;
import com.dream.wuliu.base.MyApplication;
import com.dream.wuliu.base.MyErrorHandler.AuthorizeException;
import com.dream.wuliu.base.MyErrorHandler.NetworkException;
import com.dream.wuliu.base.MyErrorHandler.RequestTimeoutException;
import com.dream.wuliu.base.MyErrorHandler.SysException;
import com.dream.wuliu.base.MyPrefs_;
import com.dream.wuliu.utils.ServiceBuilder;
import com.google.gson.Gson;

/**
 * 
 * @author EvanYu
 *
 */
@EActivity
public abstract class BaseActivity extends SherlockFragmentActivity {
	
	@App
	MyApplication myApp;
	
	@Pref
	MyPrefs_ myPrefs;
	
	@Bean
	ServiceBuilder serviceBuilder;
	
	@UiThread
	public void toastMsg(String message) {
		
		Toast.makeText(myApp.getApplicationContext(),message, Toast.LENGTH_SHORT).show();
	}
	
	@UiThread
	public void handleException(Exception ex){
		
		if(ex == null){
			return;
		}
		
		if(ex instanceof NetworkException){
			toastMsg(myApp.getStr(R.string.ex_net));
			return;
		}
		
		if(ex instanceof RequestTimeoutException){
			toastMsg(myApp.getStr(R.string.ex_timeout));
			return;
		}
		
		if(ex instanceof SysException){
			toastMsg(myApp.getStr(R.string.ex_sys));
			return;
		}
		
		if(ex instanceof AuthorizeException){
			toastMsg(myApp.getStr(R.string.not_authorize));
			return;
		}
		toastMsg(myApp.getStr(R.string.ex_req));
		
	}
	
	public String userToGson(ApiUser user ){
		if(user == null){return "";}
		return new Gson().toJson(user);
	}
	
	public ApiUser gsonToUser(String user){
		
		if(user==null || user.isEmpty()) return null;
		return new Gson().fromJson(user, ApiUser.class);
	}
	
	public void clearPre(){
		myPrefs.clear();
	}
	
	public ApiUser getLoginUser(){
		
		String str = myPrefs.user().get();
		return gsonToUser(str);
		
	}
	
	public void saveGsonUser(String user){
		myPrefs.user().put(user);
	}
	
	public  void createShortCut() {  
  
        Intent shortcutintent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");  
        shortcutintent.putExtra("duplicate", false);  
        shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_NAME, myApp.getStr(R.string.app_name));  
        Parcelable icon = Intent.ShortcutIconResource.fromContext(myApp, R.drawable.ic_app_icon);  
        
        shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);  
        shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_INTENT,new Intent(myApp, this.getClass()));
        
        this.sendBroadcast(shortcutintent);  
    }
	
	public  String getStr(int id){
		return myApp.getStr(id);
	}
	
	public ServiceBuilder getServiceBuilder(){
		return serviceBuilder;
	}
	
	public void showProgress(){
		setSupportProgressBarIndeterminateVisibility(true);
	}
	public void hideProgress(){
		setSupportProgressBarIndeterminateVisibility(false);
	}
	
	public ProgressDialog showDialogProgress(){
		ProgressDialog dialog = new ProgressDialog(this);
		dialog.setTitle(null);
		dialog.setMessage(getStr(R.string.inf_updating));
		dialog.setIndeterminate(true);
		dialog.setCanceledOnTouchOutside(false);
		dialog.show();
		return dialog;
	}
	
	public ProgressDialog showDialogProgress(String title,String msg){
		ProgressDialog dialog = new ProgressDialog(this);
		dialog.setTitle(title);
		dialog.setMessage(msg);
		dialog.setIndeterminate(true);
		dialog.setCanceledOnTouchOutside(false);
		dialog.show();
		return dialog;
	}
	
}
